package com.shop.front.controller.pink;

import com.shop.front.vo.pink.PinkJishouVo;
import com.shop.front.vo.pink.StorePinkVo;
import com.shop.front.vo.store.StoreOrderVo;
import com.shop.common.enums.TrueFalseEnum;
import com.shop.common.partner.jiaotai.JiaotaiBaseRespVo;
import com.shop.domain.pink.PinkJishou;
import com.shop.domain.pink.StorePink;
import com.shop.domain.pink.UserPinkInfo;
import com.shop.domain.user.UserPinkBill;
import com.shop.front.dao.user.UserDao;
import com.shop.front.request.pink.*;
import com.shop.front.service.pink.StorePinkService;
import com.shop.front.service.user.UserPinkBillService;
import com.shop.front.shiro.UserTokenUtil;
import com.shop.util.IdSignUtil;
import com.shop.web.cache.RedisCache;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(description = "用户拼团相关", position = -1)
@RestController
@RequestMapping(value = "/userPink", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class UserPinkController {

    @Autowired
    private StorePinkService storePinkService;

    @Autowired
    private UserPinkBillService userPinkBillService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserDao userDao;

  /*  @ApiOperation(value = "参与拼团")
    @ApiResponses(@ApiResponse(code = 10000, response = StorePink.class, message = "content"))
    @RequestMapping("/pink")
    public BaseResponse pink(@RequestBody @Validated UserPinkReq userPinkReq) {

        Long pinkId = IdSignUtil.checkSign(userPinkReq.getPinkSign());
        if(null == pinkId){
            return BaseResponse.fail("拼团类型有误！");
        }
        userPinkReq.setPinkId(pinkId);

        Long productId = IdSignUtil.checkSign(userPinkReq.getProductSign());
        if(null == productId){
            return BaseResponse.fail("请选择商品！");
        }
        userPinkReq.setProductId(productId);

        Long productAttrValueId = IdSignUtil.checkSign(userPinkReq.getProductAttrValueSign());
        if(null == productAttrValueId){
            return BaseResponse.fail("请选择商品规格！");
        }
        userPinkReq.setProductAttrValueId(productAttrValueId);

        Long userId = UserTokenUtil.getUser().getId();

        return storePinkService.pink(userId, userPinkReq);



    }

    @ApiOperation(value = "拼团提交订单")
    @ApiResponses(@ApiResponse(code = 10000, response = JiaotaiBaseRespVo.class, message = "content"))
    @RequestMapping("/pinkOrderSubmit")
    public BaseResponse pinkOrderSubmit(@RequestBody @Validated UserPinkOrderReq userPinkOrderReq) {
        Long storePinkId = IdSignUtil.checkSign(userPinkOrderReq.getStorePinkSign());
        if(null == storePinkId){
            return BaseResponse.fail("请先选择一个商品参与拼团！");
        }
        userPinkOrderReq.setStorePinkId(storePinkId);

        Long userAddressId = IdSignUtil.checkSign(userPinkOrderReq.getUserAddressSign());
        if(null == userAddressId){
            return BaseResponse.fail("请选择一个收货地址！");
        }
        userPinkOrderReq.setUserAddressId(userAddressId);

        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.pinkOrderSubmit(userId, userPinkOrderReq);
    }

    @ApiOperation(value = "同步拼团订单状态")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="orderNo", required = true, value = "订单号", paramType = "body"))
    @RequestMapping("/checkPinkPayStatus")
    public BaseResponse checkPinkPayStatus(@RequestBody @Ignore Map<String, String> map) {
        String orderNo = map.get("orderNo");
        if(StringUtils.isBlank(orderNo)) {
            return BaseResponse.fail("订单号不能为空！");
        }

        return storePinkService.checkPinkPayStatus(orderNo);
    }

    @ApiOperation(value = "我的拼团")
    @ApiResponses(@ApiResponse(code = 10000, response = StorePinkVo.class, message = "content"))
    @RequestMapping("/pagePink")
    public BaseResponse pagePink(@RequestBody @ApiIgnore Map<String, String> map) {
        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.pagePink(userId);
    }

    @ApiOperation(value = "我的拼团历史")
    @ApiResponses(@ApiResponse(code = 10000, response = StorePinkVo.class, message = "content"))
    @RequestMapping("/pagePinkHistory")
    public BaseResponse pagePinkHistory(@RequestBody @ApiIgnore Map<String, String> map) {
        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.pagePinkHistory(userId);
    }

    @ApiOperation(value = "我的寄售 ")
    @ApiResponses(@ApiResponse(code = 10000, response = PinkJishouVo.class, message = "content"))
    @RequestMapping("/pageJishou")
    public BaseResponse pageJishou(@RequestBody @ApiIgnore Map<String, String> map) {
        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.pageJishou(userId);
    }

    @ApiOperation(value = "拼团成功转寄售")
    @ApiResponses(@ApiResponse(code = 10000, response = PinkJishou.class, message = "content"))
    @RequestMapping("/pinkTojishou")
    public BaseResponse pinkTojishou(@RequestBody @Validated UserPinkToJishouReq userPinkToJishouReq) {
        Long storePinkId = IdSignUtil.checkSign(userPinkToJishouReq.getStorePinkSign());
        if(null == storePinkId){
            return BaseResponse.fail("拼团订单ID不正确");
        }
        userPinkToJishouReq.setStorePinkId(storePinkId);

        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.pinkTojishou(userId,userPinkToJishouReq);
    }


    @ApiOperation(value = "寄售列表转余额")
    @ApiResponses(@ApiResponse(code = 10000, response = PinkJishou.class, message = "content"))
    @RequestMapping("/jishouToUserBalance")
    public BaseResponse jishouToUserBalance(@RequestBody @Validated UserPinkJishouToBalanceReq userPinkJishouToBalanceReq) {
        Long pinkJishouId = IdSignUtil.checkSign(userPinkJishouToBalanceReq.getPinkJishouSign());
        if(null == pinkJishouId){
            return BaseResponse.fail("寄售ID不正确");
        }
        userPinkJishouToBalanceReq.setPinkJishouId(pinkJishouId);

        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.jishouToUserBalance(userId, userPinkJishouToBalanceReq);
    }

    @ApiOperation(value = "拼中提货")
    @ApiResponses(@ApiResponse(code = 10000, response = PinkJishou.class, message = "content"))
    @RequestMapping("/pinkGetGoods")
    public BaseResponse pinkGetGoods(@RequestBody @Validated UserPinkGetGoodsReq userPinkGetGoodsReq) {
        Long storePinkId = IdSignUtil.checkSign(userPinkGetGoodsReq.getStorePinkSign());
        if(null == storePinkId){
            return BaseResponse.fail("拼团ID不正确");
        }
        userPinkGetGoodsReq.setStorePinkId(storePinkId);

        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.pinkGetGoods(userId, userPinkGetGoodsReq);
    }

    @ApiOperation(value = "寄售改提货")
    @ApiResponses(@ApiResponse(code = 10000, response = PinkJishou.class, message = "content"))
    @RequestMapping("/jishouGetGoods")
    public BaseResponse jishouGetGoods(@RequestBody @Validated UserPinkJishouGetGoodsReq userPinkJishouGetGoodsReq) {
        Long pinkJishouId = IdSignUtil.checkSign(userPinkJishouGetGoodsReq.getPinkJishouSign());
        if(null == pinkJishouId){
            return BaseResponse.fail("寄售ID不正确");
        }
        userPinkJishouGetGoodsReq.setPinkJishouId(pinkJishouId);

        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.jishouGetGoods(userId, userPinkJishouGetGoodsReq);
    }


    @ApiOperation(value = "用户拼团信息")
    @ApiResponses(@ApiResponse(code = 10000, response = UserPinkInfo.class, message = "content"))
    @RequestMapping("/userPinkInfo")
    public BaseResponse userPinkInfo(@RequestBody @ApiIgnore Map<String, String> map) {
        Long userId = UserTokenUtil.getUser().getId();
        return storePinkService.userPinkInfo(userId,1l);
    }

    @ApiOperation(value = "拼团佣金记录")
    @ApiResponses(@ApiResponse(code = 10000, response = UserPinkBill.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="pinkSign", required = true, value = "拼团ID Sign", paramType = "body"))
    @RequestMapping("/getPinkBillLog")
    public BaseResponse getPinkBillLog(@RequestBody @Ignore Map<String, String> map) {
        Long storePinkId = IdSignUtil.checkSign(map.get("pinkSign"));
        if(null == storePinkId) {
            return BaseResponse.fail("拼团ID不能为空！");
        }
        Long userId = UserTokenUtil.getUser().getId();

        return userPinkBillService.getPinkBillLog(storePinkId,userId);
    }


    @ApiOperation(value = "取消拼团")
    @ApiResponses(@ApiResponse(code = 10000, response = StorePink.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="pinkSign", required = true, value = "拼团ID Sign", paramType = "body"))
    @RequestMapping("/cancelStorePink")
    public BaseResponse cancelStorePink(@RequestBody @Ignore Map<String, String> map) {

        Long storePinkId = IdSignUtil.checkSign(map.get("pinkSign"));
        if(null == storePinkId){
            return BaseResponse.fail("拼团ID不正确");
        }
        Long userId = UserTokenUtil.getUser().getId();

        return storePinkService.cancelStorePink(userId, storePinkId);
    }


    @ApiOperation(value = "拼团商品配置")
    @ApiResponses(@ApiResponse(code = 10000, response = StorePink.class, message = "content"))
    @RequestMapping("/updateStorePinkConfig")
    public BaseResponse updateStorePinkConfig(@RequestBody @Validated AutoUserPinkConfigReq autoUserPinkConfigReq) {

        Long userId = UserTokenUtil.getUser().getId();

        int isAutoPt = autoUserPinkConfigReq.getIsAutoPt();

        Long storePinkId = 0L;
        Long autoProductId = 0L;
        Long autoProductAttrValueId = 0L;
        Long userAddressId = 0L;

        storePinkId = IdSignUtil.checkSign(autoUserPinkConfigReq.getPinkSign());
        if(null == storePinkId){
            return BaseResponse.fail("拼团ID不正确");
        }

        if(isAutoPt == TrueFalseEnum.TRUE.getCode()){
            autoProductId = IdSignUtil.checkSign(autoUserPinkConfigReq.getAutoProductSign());
            if(null == autoProductId){
                return BaseResponse.fail("拼团商品ID不正确");
            }

            autoProductAttrValueId = IdSignUtil.checkSign(autoUserPinkConfigReq.getAutoProductAttrValueSign());
            if(null == autoProductAttrValueId){
                return BaseResponse.fail("拼团商品规格ID不正确");
            }

            userAddressId = IdSignUtil.checkSign(autoUserPinkConfigReq.getUserAddressSign());
            if(null == userAddressId){
                return BaseResponse.fail("请选择一个收货地址！");
            }
        }

        return storePinkService.updateStorePinkConfig(userId,storePinkId, autoProductId, autoProductAttrValueId, userAddressId, isAutoPt);
    }*/





}
